package com.wzy.web.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import net.sf.json.JSONObject;

import com.adobe.agl.text.SimpleDateFormat;
import com.wzy.web.pojo.PmAffix;
import com.wzy.web.pojo.PmArticle;
import com.wzy.web.pojo.PmUser;
import com.wzy.web.vo.AffixVO;
import com.wzy.web.vo.ArticleVO;
import com.wzy.web.vo.UserVO;

public class UserDataService extends BasicService {
	
	/**
	 * 验证登陆
	 * @param obj
	 * @return
	 */
	public Map<String, Object> checkLogin(Object obj) {
		Map<String, Object> reMap = new HashMap<String, Object>();
		Map<String, String> map = (Map<String, String>)obj;
		try {
			PmUser vo = daoPmUser.checkUserLogin(map.get("userName").toString(), map.get("userPass").toString());
			if(vo != null) {
				reMap.put("SORF", "true");
				reMap.put("VALUE", vo);
			} else {
				reMap.put("SORF", "false");
				reMap.put("VALUE", null);
			}
		} catch (Exception e) {
			reMap.put("SORF", "false");
			reMap.put("VALUE", null);
			e.printStackTrace();
		}
		return reMap;
	}
	/**
	 * 
	 * @descr 修改密码
	 * @author wangzy 
	 * @date 2009-12-11 
	 * @param jsonObj
	 * @return
	 */
	public Map<String, Object> changePassword(Object jsonObj) {
		Map<String, Object> reMap = new HashMap<String, Object>();
		JSONObject js = JSONObject.fromObject(jsonObj);
		try {
			PmUser vo = daoPmUser.findById(Integer.parseInt(js.getString("userId")));
			if(vo.getUserPass().equals(js.getString("oldPass"))) {
				vo.setUserPass(js.getString("newPass"));
				PmUser tempVo = daoPmUser.merge(vo);
				reMap.put("SORF", "true");
				reMap.put("VALUE", tempVo);
			} 
		} catch (Exception e) {
			reMap.put("SORF", "false");
			reMap.put("ERROR", "修改密码出现异常");
			e.printStackTrace();
		}
		return reMap;
	}
	/**
	 * 保存User
	 * @param obj
	 * @return
	 */
	public Map<String, Object> saveUser(Object obj) {
		Map<String, Object> reMap = new HashMap<String, Object>();
		Map<String, String> map = (Map<String, String>)obj;
		String sysDate = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date());
		PmUser vo = new PmUser();
		if(!map.get("userId").equals("-1")) {
			vo.setUserId(Integer.parseInt(map.get("userId")));
		}
		vo.setUserName(map.get("userName"));
		vo.setUserEmail(map.get("userEmail"));
		vo.setUserImageUrl(map.get("userImageUrl"));
		vo.setUserMobeilPhoto(map.get("userMobeilPhoto"));
		vo.setUserTelphoto(map.get("userTelphoto"));
		vo.setUserPartment(map.get("userPartment"));
		vo.setUserSex(map.get("userSex"));
		vo.setDateTime(sysDate);
		vo.setUserPass("888888");
		PmUser tempVO = daoPmUser.merge(vo);
		if(tempVO != null) {
			reMap.put("SORF", "true");
		} else {
			reMap.put("SORF", "false");
		}
		reMap.put("VALUE", tempVO);
		return reMap;
	}
	/**
	 * load所有用户
	 * @return
	 */
	public Map<String, Object> findAllUser() {
		Map<String, Object> reMap = new HashMap<String, Object>();
		List<PmUser> list = new ArrayList<PmUser>();
		try {
			list = daoPmUser.findAll();
			reMap.put("SORF", "true");
			reMap.put("VALUE", list);
		} catch (Exception e) {
			e.printStackTrace();
			reMap.put("SORF", "false");
		}
		return reMap;
	}
	/**
	 * load指定ID用户
	 * @param userId
	 * @return
	 */
	public Map<String, Object> findUserById(String userId) {
		Map<String, Object> reMap = new HashMap<String, Object>();
		try {
			PmUser vo = daoPmUser.findById(Integer.parseInt(userId));
			UserVO reVo = new UserVO();
			if(vo != null) {
				reVo.setDateTime(vo.getDateTime());
				reVo.setUserEmail(vo.getUserEmail());
				reVo.setUserId(vo.getUserId().toString());
				reVo.setUserImageUrl(vo.getUserImageUrl());
				reVo.setUserMobeilPhoto(vo.getUserMobeilPhoto());
				reVo.setUserName(vo.getUserName());
				reVo.setUserPartment(vo.getUserPartment());
				reVo.setUserPass(vo.getUserPass());
				reVo.setUserSex(vo.getUserSex());
				reVo.setUserTelphoto(vo.getUserTelphoto());
				Set<PmAffix> affixSet = vo.getPmAffixes();
				Set<PmArticle> articleSet = vo.getPmArticles();
				List affixList = new ArrayList();
				List aritcleList = new ArrayList();
				if(affixSet != null) {
					Iterator<PmAffix> affixIT = affixSet.iterator();
					while(affixIT.hasNext()) {
						PmAffix affixVo = affixIT.next();
						AffixVO affVo = new AffixVO();
						if(affixVo != null) {
							affVo.setAffixId(affixVo.getAffixId().toString());
							affVo.setAffixName(affixVo.getAffixName());
							affVo.setAffixDetail(affixVo.getAffixDetail());
							affVo.setAffixUrl(affixVo.getAffixUrl());
							affVo.setDateTime(affixVo.getDateTime());
							affVo.setUserName(affixVo.getPmUser().getUserName());
						}
						affixList.add(affVo);
					}
				}
				reVo.setPmAffixes(affixList);
				if(aritcleList != null) {
					Iterator<PmArticle> articleIT = articleSet.iterator();
					ArticleVO artVo = null;
					while (articleIT.hasNext()) {
						PmArticle articleVo = articleIT.next();
						artVo = new ArticleVO();
						artVo.setArticleContent(articleVo.getArticleContent());
						artVo.setArticleId(articleVo.getArticleId().toString());
						artVo.setArticleName(articleVo.getArticleName());
						artVo.setDateTime(articleVo.getDateTime());
						artVo.setUserName(articleVo.getPmUser().getUserName());
						artVo.setProjectId(articleVo.getPmProject().getProjectId().toString());
						artVo.setUserId(articleVo.getPmUser().getUserId().toString());
						aritcleList.add(artVo);
					}
				}
				reVo.setPmArticles(aritcleList);
				reMap.put("SORF", "true");
			} else {
				reMap.put("SORF", "false");
			}
			reMap.put("VALUE", reVo);
		} catch (Exception e) {
			e.printStackTrace();
			reMap.put("SORF", "false");
		}
		return reMap;
	}
	/**
	 * 删除指定用户
	 * @param userId
	 * @return
	 */
	public Map<String, Object> deleteUserById(String userId) {
		Map<String, Object> reMap = new HashMap<String, Object>();
		daoPmUser.delete(daoPmUser.findById(Integer.parseInt(userId)));
		reMap.put("SORF", "true");
		return reMap;
	}
}
